package com.lqk.fbridge

import android.app.Application
import android.util.Log
import io.flutter.FlutterInjector
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.FlutterEngineCache
import io.flutter.embedding.engine.FlutterEngineGroup
import io.flutter.embedding.engine.FlutterJNI
import io.flutter.embedding.engine.dart.DartExecutor

/**
 * flutter 统一管理
 */
object FlutterHelper {

    const val TAG = "FlutterHelper"

    var flutterEngine: FlutterEngine? = null
//    var flutterEngineGroup: FlutterEngineGroup? = null

    fun initFlutterHelper(application: Application) {
        // 添加默认 main
        addEngine(application)
        // twoMain
        createRoot(application)
    }

    fun createRoot(application: Application) {
        // 添加 towMain
        flutterEngine = FlutterEngine(application)
//        flutterEngineGroup = FlutterEngineGroup(application)
        // 设置默认 路由名称
        flutterEngine?.navigationChannel?.setInitialRoute("root")
        // 创建 挂载点 指定 函数名
//        flutterEngine?.dartExecutor?.executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault())
        flutterEngine?.dartExecutor?.executeDartEntrypoint(createPoint("twoMain"))

        FlutterEngineCache.getInstance().put("root", flutterEngine)
    }

    /**
     * 添加 engine
     */
    fun addEngine(application: Application, func: String = "") {
        val key = func.ifBlank {
            "default"
        }
        if (FlutterEngineCache.getInstance().get(key) == null) {
            val fe = FlutterEngine(application)
            fe.dartExecutor.executeDartEntrypoint(createPoint(func))
            FlutterEngineCache.getInstance().put(key, fe)
        }
    }

    /**
     * 创建挂载点
     */
    fun createPoint(filePath: String? = null, funcName: String? = null) =
        if (funcName == null || funcName == "") {
            Log.d(TAG, "createPoint: 创建默认 挂载点")
            DartExecutor.DartEntrypoint.createDefault()
        } else {
            Log.d(
                TAG,
                "createPoint: ${FlutterInjector.instance().flutterLoader().findAppBundlePath()}"
            )
            Log.d(TAG, "createPoint: ${FlutterJNI.getObservatoryUri()}")
            Log.d(TAG, "createPoint: ${FlutterJNI.getVMServiceUri()}")
            if (filePath != null && filePath != "") {
                DartExecutor.DartEntrypoint(
                    FlutterInjector.instance().flutterLoader().findAppBundlePath(),
                    filePath,
                    funcName
                )
            } else {
                DartExecutor.DartEntrypoint(
                    FlutterInjector.instance().flutterLoader().findAppBundlePath(),
                    funcName
                )
            }


        }

    /**
     * 获取已经实例化的 FlutterEngine
     */
    fun gainEngineByName(func: String): FlutterEngine? {
        val key = func.ifBlank {
            "default"
        }
        val fe = FlutterEngineCache.getInstance().get(key)
        return fe ?: flutterEngine
    }
}